var win = $(window),
  nav_on = null;
$(function() {
  // 导航栏控制
  (function() {
    var nav = $('#nav'),
      shop = $('#shop'),
      // search_box = shop.find('#searchbox'),
      // search_btn = shop.find('.btn-search')[0],
      lis = nav.children(),
      lis_1 = lis.filter(':not(.more)'),
      lis_2 = lis.filter('.more'),
      links = lis.children(),
      links_1 = lis_1.children(),
      links_2 = lis_2.children(),
      subNav = $('#subNav'),
      subitem = $('#subNav').find('.item'),
      prev_item = $(),
      spans = links.children(),
      offs = spans.filter('.off'),
      ons = spans.filter('.on'),
      sbs = spans.filter('.slideBlock'),

      hei = links.eq(0).height(),
      len = lis.length,

      // 记录当前
      link_page = null,
      link_curr = null,

      timeout = -1;

    // 初始化当前链接
    href = location.href.replace(/[_\d]{1,2}\./, '.'); // 静态页面用
    // href = location.href,                                   // 程序用
    // 导航栏变色模块分类
    if (href == "file:///home/qirong/Workspaces/sdqrgq/socialJobs.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/campusJobs.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/traineeCampusJobs.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/businessManagerSocialJobs.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/districtManagerSocialJobs.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/salesManagerSocialJobs.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/trainingInstructorSocialJobs.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/branchManagerSocialJobs.html") {
      href = "file:///home/qirong/Workspaces/sdqrgq/jobs.html";
    } else if (href == "file:///home/qirong/Workspaces/sdqrgq/greenlandCityLandPandect.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/jinkeCityLandPandect.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/crecLandPandect.html" ||
      href == "file:///home/qirong/Workspaces/sdqrgq/landPandect.html") {
      href = "file:///home/qirong/Workspaces/sdqrgq/business.html";
    }
    for (var i = 0; i < len; i++) {
      link_page = links.eq(i);
      if (href.indexOf(link_page.attr('href').replace(/(?:_\d)?\..*/, '')) > 0) { // 静态页面用
        // if (href.indexOf(link_page.attr('href').replace(/(?:_\d)?\..*/, '')) > 0) {    // 程序用
        control(nav_on = link_curr = link_page = link_page[0], false);
        delete i;
        break;
      }
    }
    links_2.each(function(idx) {
      if (this === nav_on) return;
      this.setAttribute('idx', idx);
    });
    if (i === len) {
      if (href.indexOf('/user') >= 0) {
        control(nav_on = link_curr = link_page = links.eq(5)[0], false);
      } else {
        control(nav_on = link_curr = link_page = links.eq(0)[0], false);
      }
    }


    win.on('load', function() {
      // 鼠标指向, 链接高亮
      links_1.hover(function() {
        control(this, false)
      }, none);
      links_2.hover(function() {
        control(this, true)
      }, none);
      // 鼠标离开导航栏, 恢复当前页面高亮
      nav.hover(none, function() {
        timeout = setTimeout(function() {
          control(link_page, true);
        }, 10);
      });
      subNav.hover(function() {
        clearTimeout(timeout);
      }, function() {
        control(link_page, true);
      });
    });

    function control(elem, flag, idx) {
      link_curr.className = "";
      elem.className = "on";
      link_curr = elem;
      prev_item.removeClass('on');
      if (flag) {
        idx = parseInt(elem.getAttribute('idx'));
        prev_item = subitem.eq(idx).addClass('on');
        // search_box.hide();
        // search_btn.className = "btn-search";
      }
    }

    function none() {}


    // 2015.06.09 修改搜索, 添加语言
    var subitem_search = subitem.filter('.search'),
      subitem_langs = subitem.filter('.langs');
    subitem_nwmenu = subitem.filter('.nwmenu');
    shop.find('.btn-search').hover(function() {
      prev_item.removeClass('on');
      prev_item = subitem_search.addClass('on');
    }, none);
    shop.find('.btn-lang').hover(function() {
      prev_item.removeClass('on');
      prev_item = subitem_langs.addClass('on');
    }, none);
    shop.find('.header-nwbtn').hover(function() {
      prev_item.removeClass('on');
      prev_item = subitem_nwmenu.addClass('on');
    }, none);

    shop.hover(none, function() {
      timeout = setTimeout(function() {
        prev_item.removeClass('on');
      }, 300);
    });

    subitem_nwmenu.on('mouseleave', function() {
      prev_item.removeClass('on');
    })
  }());

  // 搜索按钮
  (function() {
    var sup = $('#header'),
      btn = sup.find('.btn-search'),
      box = sup.find('#searchbox'),
      timeout = -1,
      delay = 1000;

    btn.hover(open, autoFade);
    box.on('focus', function() {
      clearTimeout(timeout);
    }).on('blur', function() {
      autoFade();
    });

    function open() {
      clearTimeout(timeout);
      btn.addClass('on');
      box.stop(false, true).fadeIn(300);
    }

    function close() {
      btn.removeClass('on');
      box.stop(false, true).fadeOut(300);
    }

    function autoFade() {
      timeout = setTimeout(function() {
        close();
      }, delay);
    }
  }());


  // placeholder 处理
  (function() {
    // 情况1，如果浏览器支持 placeholder，则优先使用默认。
    var input = document.createElement("input");
    input.type = "text";
    if ("placeholder" in input) return;

    // 情况2，如果没有任何标签使用 placeholder，则不做任何处理。
    var inputs = $("*[placeholder]"),
      val = "";
    if (!inputs.length) return;

    // 情况3，对于不支持 placeholder 属性的元素进行预加载和事件挂载。
    inputs
      .each(function() {
        input = $(this);
        input.val(input.attr("placeholder"));
      })
      .on("focus", focus)
      .on("blur", blur)
      .on("change", change);

    // 处理函数
    function focus() {
      input = $(this);
      val = input.val();
      if (val && (val !== input.attr('placeholder'))) return;
      else input.val("");
    }

    function blur() {
      // input.val(input.attr("placeholder"));
      val = input.val();
      if (val && (val !== input.attr('placeholder'))) {
        return;
      } else {
        input.val(input.attr("placeholder"));
      }
    }

    function change() {
      if (input.val()) {
        input.off("focus").off("blur");
      } else {
        input.on("focus", focus).on("blur", blur);
      }
    }
  })();

  // 调整高度
  (function() {
    var copyright = $('#copyright'),
      hei = $('html').height() - $('body').height();
    if (hei <= 0) return;
    copyright.find('.g-wrap div').height(37 + hei);
  }());

});
